import i18n
import wx
import models
import db
import datetime
from dbgrid import DBGrid
from setting import Session
from ctrls import TooltipText, AutoCompleteText

special_chars = [u'\u00b1', u'\u00d7', u'\u0424', u'\u0394']

class MainFrame(wx.Frame):
	def __init__(self, parent, id):
		wx.Frame.__init__(self, parent, id, _("Copper"), size=(800, 600))

		self.init_widget()
		self.init_layout()
		self.event_map()

	def create_text(self, *args, **kwargs):
		return wx.TextCtrl(self.panel, -1, *args, **kwargs)

	def create_static(self, *args, **kwargs):
		return wx.StaticText(self.panel, -1, *args, **kwargs)

	def create_tooltip_text(self, **kwargs):
		kwargs["choices"] = special_chars
		return TooltipText(self.panel, **kwargs) 

	def init_widget(self):
		self.panel = panel = wx.Panel(self, -1)
		self.save_button = wx.Button(panel, -1, _("Save"))
		self.new_button = wx.Button(panel, -1, _("New"))

		self.date_static = self.create_static(_("Date"), style=wx.ALIGN_CENTER)
		self.date_text = self.create_text(self.now_text())

		self.custom_static = self.create_static(_("Custom"), style=wx.ALIGN_CENTER)
		self.custom_text = self.create_text(_("Custom"))


		self.grade_static = self.create_static(_("Grade"), style=wx.ALIGN_CENTER)
		self.grade_text = self.create_text(_("Grade"))

		self.shape_static = self.create_static(_("Shape"), style=wx.ALIGN_CENTER)
		self.shape_text = self.create_text(_("Shape"))

		self.rough_spec_text = self.create_text(_("Rough"))
		self.rough_spec_text.Disable()
		self.rough_od_static = self.create_static(_("Rough ODia"), style=wx.ALIGN_CENTER)
		self.rough_od_text = self.create_tooltip_text()
		self.rough_id_static = self.create_static(_("Rough IDia"), style=wx.ALIGN_CENTER)
		self.rough_id_text = self.create_tooltip_text()
		self.rough_len_static = self.create_static(_("Rough Length"), style=wx.ALIGN_CENTER)
		self.rough_len_text = self.create_tooltip_text()
		self.rough_wlt_static = self.create_static(_("Rough WLT"), style=wx.ALIGN_CENTER)
		self.rough_wlt_text = self.create_tooltip_text()
		self.rough_odt_static = self.create_static(_("Rough ODT"), style=wx.ALIGN_CENTER)
		self.rough_odt_text = self.create_tooltip_text()
		self.rough_idt_static = self.create_static(_("Rough IDT"), style=wx.ALIGN_CENTER)
		self.rough_idt_text = self.create_tooltip_text()
		self.rough_lt_static = self.create_static(_("Rough LT"), style=wx.ALIGN_CENTER)
		self.rough_lt_text = self.create_tooltip_text()
		self.weight_static = self.create_static(_("Weight"), style=wx.ALIGN_CENTER)
		self.weight_text = self.create_text("0.0")
		self.status_static = self.create_static(_("Status"), style=wx.ALIGN_CENTER)
		self.status_text = self.create_text(_("Status"))

		self.product_od_static = self.create_static(_("Product ODia"), style=wx.ALIGN_CENTER)
		self.product_od_text = self.create_tooltip_text()
		self.product_id_static = self.create_static(_("Product IDia"), style=wx.ALIGN_CENTER)
		self.product_id_text = self.create_tooltip_text()
		self.product_len_static = self.create_static(_("Product Length"), style=wx.ALIGN_CENTER)
		self.product_len_text = self.create_tooltip_text()

		self.ingot_len_static = self.create_static(_("Ingot Length"), style=wx.ALIGN_CENTER)
		self.ingot_len_text = self.create_text(_("Ingot Length"))
		self.extrusion_model_static = self.create_static(_("Extrusion model"), style=wx.ALIGN_CENTER)
		self.extrusion_model_text = self.create_text(_("Extrusion model"))
		self.count_static = self.create_static(_("Count"), style=wx.ALIGN_CENTER)
		self.count_text = self.create_text(_("Count"))

		self.memo_text = self.create_text("", size=(-1, -1), style=wx.TE_MULTILINE | wx.TE_PROCESS_ENTER)

		self.data_grid = DBGrid(panel, models.ExtrusionOrder)

	def init_layout(self):
		panel = self.panel
		self.main_sizer = main_sizer = wx.GridBagSizer(16,16)

		product_box = wx.StaticBox(panel, -1, _("Base Info"))
		box_sizer = wx.StaticBoxSizer(product_box, wx.VERTICAL)
		gbs = wx.GridBagSizer(2,2)
		gbs.Add(self.date_static, (0, 0))
		gbs.Add(self.date_text, (0, 1))
		gbs.Add(self.custom_static, (1, 0))
		gbs.Add(self.custom_text, (1, 1))
		box_sizer.Add(gbs, 1, wx.EXPAND | wx.ALIGN_CENTER | wx.ALL, 2)
		main_sizer.Add(box_sizer, (0, 0), (3, 2), wx.EXPAND | wx.ALIGN_CENTER | wx.ALL, 2)
		main_sizer.Add(self.save_button, (0, 8))
		main_sizer.Add(self.new_button, (1, 8))

		grade_box = wx.StaticBox(panel, -1, _("Grade"))
		box_sizer = wx.StaticBoxSizer(grade_box, wx.VERTICAL)
		gbs = wx.GridBagSizer(2,2)
		gbs.Add(self.grade_static, (0, 0))
		gbs.Add(self.grade_text, (0, 1))
		gbs.Add(self.shape_static, (1, 0))
		gbs.Add(self.shape_text, (1, 1))
		box_sizer.Add(gbs, 1, wx.EXPAND | wx.ALIGN_CENTER | wx.ALL, 2)
		main_sizer.Add(box_sizer, (3, 0), (2, 2), wx.EXPAND | wx.ALIGN_CENTER | wx.ALL, 2)

		rough_box = wx.StaticBox(panel, -1, _("Rough"))
		box_sizer = wx.StaticBoxSizer(rough_box, wx.VERTICAL)
		gbs = wx.GridBagSizer(5, 2)
		gbs.Add(self.rough_spec_text, (0, 0), (1, 2), wx.EXPAND | wx.ALIGN_CENTER)
		gbs.Add(self.rough_wlt_static, (0, 2))
		gbs.Add(self.rough_wlt_text, (0, 3))
		gbs.Add(self.rough_od_static, (1, 0))
		gbs.Add(self.rough_od_text, (1, 1))
		gbs.Add(self.rough_odt_static, (1, 2))
		gbs.Add(self.rough_odt_text, (1, 3))
		gbs.Add(self.rough_id_static, (2, 0))
		gbs.Add(self.rough_id_text, (2, 1))
		gbs.Add(self.rough_idt_static, (2, 2))
		gbs.Add(self.rough_idt_text, (2, 3))
		gbs.Add(self.rough_len_static, (3, 0))
		gbs.Add(self.rough_len_text, (3, 1))
		gbs.Add(self.rough_lt_static, (3, 2))
		gbs.Add(self.rough_lt_text, (3, 3))
		gbs.Add(self.weight_static, (4, 0))
		gbs.Add(self.weight_text, (4, 1))
		gbs.Add(self.status_static, (4, 2))
		gbs.Add(self.status_text, (4, 3))
		box_sizer.Add(gbs, 1, wx.EXPAND | wx.ALIGN_CENTER | wx.ALL, 15)
		main_sizer.Add(box_sizer, (0, 2), (5, 5), wx.EXPAND | wx.ALIGN_CENTER | wx.ALL, 2)

		product_box = wx.StaticBox(panel, -1, _("Product"))
		box_sizer = wx.StaticBoxSizer(product_box, wx.VERTICAL)
		gbs = wx.GridBagSizer(3,2)
		gbs.Add(self.product_od_static, (0, 0))
		gbs.Add(self.product_od_text, (0, 1))
		gbs.Add(self.product_id_static, (1, 0))
		gbs.Add(self.product_id_text, (1, 1))
		gbs.Add(self.product_len_static, (2, 0))
		gbs.Add(self.product_len_text, (2, 1))
		box_sizer.Add(gbs, 1, wx.EXPAND | wx.ALIGN_CENTER | wx.ALL, 2)
		main_sizer.Add(box_sizer, (5, 0), (3, 2), wx.EXPAND | wx.ALIGN_CENTER | wx.ALL, 2)

		process_box = wx.StaticBox(panel, -1, _("Process Setting"))
		box_sizer = wx.StaticBoxSizer(process_box, wx.VERTICAL)
		gbs = wx.GridBagSizer(3,2)
		gbs.Add(self.ingot_len_static, (0, 0))
		gbs.Add(self.ingot_len_text, (0, 1))
		gbs.Add(self.extrusion_model_static, (1, 0))
		gbs.Add(self.extrusion_model_text, (1, 1))
		gbs.Add(self.count_static, (2, 0))
		gbs.Add(self.count_text, (2, 1))
		box_sizer.Add(gbs, 1, wx.EXPAND | wx.ALIGN_CENTER | wx.ALL, 2)
		main_sizer.Add(box_sizer, (5, 2), (3, 2), wx.EXPAND | wx.ALIGN_CENTER | wx.ALL, 2)

		memo_box = wx.StaticBox(panel, -1, _("Memo"))
		box_sizer = wx.StaticBoxSizer(memo_box, wx.VERTICAL)
		box_sizer.Add(self.memo_text, 1, wx.EXPAND)
		main_sizer.Add(box_sizer, (5, 4), (3, 3), wx.EXPAND | wx.ALIGN_CENTER | wx.ALL, 2)

		main_sizer.Add(self.data_grid, (8, 0), (7, 9), wx.EXPAND | wx.ALIGN_CENTER)

		box = wx.BoxSizer()
		box.Add(main_sizer, 0, wx.ALL, 10)
		panel.SetSizerAndFit(box)
		panel.SetClientSize(panel.GetSize())
	
	def event_map(self):
		self.Bind(wx.EVT_BUTTON, self.OnNewButton, self.new_button)
		self.Bind(wx.EVT_BUTTON, self.OnSaveButton, self.save_button)

		self.date_text.Bind(wx.EVT_KILL_FOCUS, self.OnDateLeave)

		self.Bind(wx.EVT_TEXT, self.OnRoughChanged, self.rough_od_text)
		self.Bind(wx.EVT_TEXT, self.OnRoughChanged, self.rough_id_text)
		self.Bind(wx.EVT_TEXT, self.OnRoughChanged, self.rough_len_text)

	def OnDateLeave(self, evt):
		self.date_text.Value = str(self.parse_date(self.date_text.Value))

	def OnRoughChanged(self, evt):
		t = self.rough_od_text.Value.strip()
		id_text = self.rough_id_text.Value.strip()
		if id_text:
			t += "*%s" % id_text
		len_text = self.rough_len_text.Value.strip()
		if len_text:
			t += "*%s" % len_text

		self.rough_spec_text.Value = t
		evt.Skip()

	def OnSaveButton(self, evt):
		session = Session()
		try:
			custom = db.get_custom(self.custom_text.Value, session)
			grade = db.get_grade(self.grade_text.Value, session)
			date = self.parse_date(self.date_text.Value)
			weight = float(self.weight_text.Value.strip())
			shape = self.shape_text.Value.strip()
			rough_spec = self.rough_spec_text.Value.strip()
			status = self.status_text.Value.strip()
			odt = self.rough_odt_text.Value.strip()
			idt = self.rough_idt_text.Value.strip()
			lt = self.rough_id_text.Value.strip()
			wt = self.rough_wlt_text.Value.strip()
			product_spec = self.get_product_spec()
			ingot_length = self.ingot_len_text.Value.strip()
			extrusion_die = self.extrusion_model_text.Value.strip()
			count = self.count_text.Value.strip()
			memo = self.memo_text.Value.strip()

			order = models.ExtrusionOrder(custom, date, grade, weight=weight,
					shape=shape, rough_spec=rough_spec, status=status,
					odt=odt, idt=idt, lt=lt, wt=wt, product_spec=product_spec,
					ingot_length=ingot_length, extrusion_die=extrusion_die,
					count=count, memo=memo)
			session.add(order)
			session.commit()
		except ValueError:
			print "Value, Error"
			session.rollback()
		except:
			print "database, Error"
			session.rollback()
		else:
			print "success insert data"
			self.data_grid.Reset()

	def OnNewButton(self, evt):
		for i in xrange(8):
			for j in xrange(8):
				print self.main_sizer.GetCellSize(i, j),
			print

	def now_text(self):
		now = datetime.datetime.now()
		return "%s-%s-%s" % (now.year, now.month, now.day)

	def parse_date(self, text):
		entries = text.strip().split("-")
		now = datetime.datetime.now()

		if len(entries) == 1:
			year = now.year
			month = now.month
			day = int(entries[0].strip())
		elif len(entries) == 2:
			year = now.year
			month = int(entries[0].strip())
			day = int(entries[1].strip())
		elif len(entries) == 3:
			year = int(entries[0].strip())
			month = int(entries[1].strip())
			day = int(entries[2].strip())

		return datetime.date(year, month, day)

	def get_product_spec(self):
		ret = self.product_od_text.Value.strip()
		id_text = self.product_id_text.Value.strip()
		if id_text:
			ret += "*%s" % id_text
		len_text = self.product_len_text.Value.strip()
		if len_text:
			ret += "*%s" % len_text
		return ret

	def get_rough_spec(self):
		ret = self.rough_od_text.Value.strip()
		id_text = self.rough_id_text.Value.strip()
		if id_text:
			ret += "*%s" % id_text
		len_text = self.rough_len_text.Value.strip()
		if len_text:
			ret += "*%s" % len_text
		return ret


if __name__ == '__main__':
	i18n.init_i18n()
	app = wx.PySimpleApp(False)
	win = MainFrame(None, -1)
	win.Show(True)
	app.MainLoop()
